package com.qiche.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.qiche.config.YmlConfig;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 车牌识别工具类
 *
 * @author zhangds
 * @since 2017年08月30日 11时36分31秒
 **/
@Component
public class LicensePateUtils {

    @Autowired
    private YmlConfig ymlConfig;
    public JSONObject Recognition(String img){
     //   JsonObject res = new JsonObject();

        JSONObject res =new JSONObject();
        String host = "http://aliyun-api.paipaitingche.com";
        String path = "/chepai_aliyun/";
        String method = "POST";
        //String appcode = PropertiesUtils.getPropertiesValueAsString("aliyun.pate.appcode");
        String appcode=ymlConfig.getAliyunPateAppcode();
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("image", img);


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = AliyunHttpUtils.doPost(host, path, method, headers, querys, bodys);
           String jsons= EntityUtils.toString(response.getEntity());
           // res = new Gson().fromJson(jsons,JsonObject.class);
            res= JSON.parseObject(jsons);
            System.out.println(res.toJSONString());

            //获取response的body
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }

    public static  void main(String[] args){
//        String strImg = ImageUtils.getImageStr("C:\\Users\\Administrator\\Desktop\\timg1.jpg");
//        JsonObject recognition = new LicensePateUtils().Recognition(strImg);
        JsonObject recognition = new LicensePateUtils().selectUserByOpenidAndCardCode("oRF1owbKfVTsZYU97TuCrkvVdEvA1","KyvbgfQB0gxtNdV2");
        boolean haveLottery = false;
        //查询该卡券是否已经抽奖
        if(recognition.get("rows")!= null){
            JsonObject rows = recognition.get("rows").getAsJsonObject();
            if(rows.get("residueDegree") != null){
                String residueDegreeStr = rows.get("residueDegree").getAsString();
                int residueDegree = Integer.parseInt(residueDegreeStr);
                //如果此卡券的抽奖次数大于0，则显示已经抽奖
                if(residueDegree > 0 ){
                    haveLottery = true;
                }
            }
        }
        //为空的时，这个卡券并未参与抽奖抽奖
        if(recognition.get("rows")== null){
            haveLottery = true;
        }

        System.out.println(haveLottery);
    }


    public JsonObject selectUserByOpenidAndCardCode(String openid, String cardCode){
        JsonObject res = new JsonObject();
        String host = PropertiesUtils.getPropertiesValueAsString("game.api.url");
        String path = "/rest/turntableSystem/selectUserByOpenidAndCardCode";
        String method = "POST";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();

        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("openid",openid);
        bodys.put("cardCode",cardCode);

        try {
            HttpResponse response = AliyunHttpUtils.doPost(host, path, method, headers, querys, bodys);
            res = new Gson().fromJson(EntityUtils.toString(response.getEntity()),JsonObject.class);
            //获取response的body
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }

}
